package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.EUDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.JsonUtils;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.service.ItemDescService;
import com.taotao.service.ItemService;


/****
 * 商品管理controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/item")
public class ItemController {

	//商品管理service
	@Autowired
	private ItemService itemService;
	//商品描述service
	@Autowired
	private ItemDescService itemDescService;

	/***
	 * 根据id返回
	 * @param itemId
	 * @return
	 */
	@RequestMapping("/{itemId}")
	@ResponseBody
	public TbItem getItemById(@PathVariable long itemId){
		TbItem tbItem = itemService.getItemById(itemId);
		return tbItem;
	}

	/**
	 * 分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public EUDataGridResult getItemList(Integer page, Integer rows) {
		EUDataGridResult result = itemService.getItemList(page, rows);
		return result;
	}
	
	/***
	 * 保存商品
	 * @param item 商品
	 * @param desc 商品描述
	 * @param itemParams 商品规格参数
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult saveItem(TbItem item, String desc,String itemParams) throws Exception {
		//添加商品信息
		TaotaoResult result = itemService.saveItem(item, desc, itemParams);
		return result;
	}
	
	
	
	
	@RequestMapping("/delete")
	@ResponseBody
	public TaotaoResult deleteItem(String ids) throws Exception {
		//添加商品信息
		itemService.deleteItemByIds(ids);
		return TaotaoResult.ok();
	}
	
	
	//-------------商品描述-----------
	
	
	/***
	 * 根据商品id查询商品描述
	 * @param itemId
	 * @return
	 */
	@RequestMapping("/itemDesc/query/{itemId}")
	@ResponseBody
	public TaotaoResult getItemDescById(@PathVariable long itemId){
		TbItemDesc itemDesc = itemDescService.getItemDescById(itemId);
		return TaotaoResult.ok(itemDesc);
	}
}
